package com.jy.rhin.application.assembler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.org.chiss.rhin._2015.base.Address;
import cn.org.chiss.rhin._2015.base.HumanName;
import cn.org.chiss.rhin._2015.base.Identifier;
import cn.org.chiss.rhin._2015.base.TelecomModePrimitive;
import cn.org.chiss.rhin._2015.base.object.Patient;
import cn.org.chiss.rhin._2015.base.object.Person;
import cn.org.chiss.rhin._2015.registration.person.PersonRecordFeed;
import com.jy.api.patient.dto.BingRenXXDto;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

public class PersonInfoAssembler {
    private PersonInfoAssembler() {
    }

    public static BingRenXXDto toBingRenXXDto(PersonRecordFeed personRecordFeed) {
        Patient personInfo = personRecordFeed.getPersonInfo();
        BingRenXXDto bingRenXXDto = new BingRenXXDto();
//        setAddress(personInfo.getAddress(), bingRenXXDto);

        // 联系方式
        if (personInfo.getTelecom() != null && personInfo.getTelecom().getUse() != null) {
            TelecomModePrimitive value = personInfo.getTelecom().getUse().getValue();
            if (TelecomModePrimitive.TELEPHONE.equals(value) || TelecomModePrimitive.CELLPHONE.equals(value)) {
                bingRenXXDto.setShouJiHao(personInfo.getTelecom().getValue().getValue());
            }
        }

        Person assignedPerson = personInfo.getAssignedPerson();
        if (assignedPerson.getId() != null && StrUtil.isNotBlank(assignedPerson.getId().getValue())) {
            bingRenXXDto.setEmpi(assignedPerson.getId().getValue());
        }
        if (CollUtil.isNotEmpty(assignedPerson.getIdentifier())) {
            List<Identifier> identifierList = assignedPerson.getIdentifier();
            Identifier identifier = identifierList.get(0);
            if (Objects.nonNull(identifier) && Objects.nonNull(identifier.getValue())) {
                bingRenXXDto.setZhengJianHM(identifier.getValue().getValue());
            }
            if (Objects.nonNull(identifier) && Objects.nonNull(identifier.getSystem())) {
                switch(identifier.getSystem().getValue()){
                    case "2.16.156.10011.1.3":
                        //身份证号
                        bingRenXXDto.setZhengJianLX("01");
                        break;
                    case "2.16.156.10011.1.2":
                        //健康档案号

                    case "2.16.156.10011.1.15":
                        //医保卡号

                    case "2.16.156.10011.1.19":
                        //健康卡号
                    default:
                        bingRenXXDto.setZhengJianLX("99");
                }
            }
        }
        if (CollUtil.isNotEmpty(assignedPerson.getName())) {
            List<HumanName> nameList = assignedPerson.getName();
            HumanName humanName = nameList.get(0);
            if (Objects.nonNull(humanName) && Objects.nonNull(humanName.getValue())) {
                bingRenXXDto.setBingRenXM(humanName.getValue());
            }
        }
        bingRenXXDto.setChuShengRQ(assignedPerson.getBirthTime() == null ? null : assignedPerson.getBirthTime().getValue());
        bingRenXXDto.setXingBieDM(assignedPerson.getGender() == null ? null : assignedPerson.getGender().getValue());
        if (CollUtil.isNotEmpty(assignedPerson.getAddress())) {
            if (assignedPerson.getAddress().get(0) != null) {
                setHuJiDZ(assignedPerson.getAddress().get(0), bingRenXXDto);
            }
            if (assignedPerson.getAddress().size() >1 && assignedPerson.getAddress().get(1) != null) {
                setJuZhuDZ(assignedPerson.getAddress().get(1), bingRenXXDto);
            }
        }
        if (!CollectionUtils.isEmpty(assignedPerson.getTelecom())) {
            bingRenXXDto.setLianXiRenSJ(assignedPerson.getTelecom().get(0).getValue() == null?
                    null : assignedPerson.getTelecom().get(0).getValue().getValue());
        }

        if (assignedPerson.getBirthPlace() != null) {
            setChuShengDZ(assignedPerson.getBirthPlace(), bingRenXXDto);
        }
        bingRenXXDto.setHunYinZT(assignedPerson.getMaritalStatusCode() == null ? null : assignedPerson.getMaritalStatusCode().getValue());
        if (assignedPerson.getPhoto() != null && assignedPerson.getPhoto().getValue() != null) {
            bingRenXXDto.setTouxiang(new java.lang.String(assignedPerson.getPhoto().getValue()));
        }
        return bingRenXXDto;
    }

    private static void setJuZhuDZ(Address address, BingRenXXDto bingRenXXDto) {
        bingRenXXDto.setJuZhuDSFMC(address.getState().getValue());
        bingRenXXDto.setJuZhuDSMC(address.getCity().getValue());
        bingRenXXDto.setJuZhuDQMC(address.getDistrict().getValue());
        bingRenXXDto.setJuZhuDJDMC(address.getTown().getValue());
        bingRenXXDto.setJuZhuDSQMC(address.getStreet().getValue());
        bingRenXXDto.setJuZhuDMPH(address.getHouseNumber().getValue());
    }

    private static void setHuJiDZ(Address address, BingRenXXDto bingRenXXDto) {
        bingRenXXDto.setHuKouSFMC(address.getState() == null? null : address.getState().getValue());
        bingRenXXDto.setHuKouSMC(address.getCity() == null? null : address.getCity().getValue());
        bingRenXXDto.setHuKouQMC(address.getDistrict() == null? null : address.getDistrict().getValue());
        bingRenXXDto.setHuKouJDMC(address.getTown() == null? null : address.getTown().getValue());
        bingRenXXDto.setHuKouSQMC(address.getStreet() == null? null : address.getStreet().getValue());
        bingRenXXDto.setHuKouDZYB(address.getPostalCode() == null? null : address.getPostalCode().getValue());
        bingRenXXDto.setHuKouDZ(address.getText() == null? null : address.getText().getValue());
    }

    private static void setChuShengDZ(Address address, BingRenXXDto bingRenXXDto) {
        bingRenXXDto.setChuShengDSFMC(address.getState().getValue());
        bingRenXXDto.setChuShengDSMC(address.getCity().getValue());
        bingRenXXDto.setChuShengDQMC(address.getDistrict().getValue());
        bingRenXXDto.setChuShengDJDMC(address.getTown().getValue());
        bingRenXXDto.setChuShengDSQMC(address.getStreet().getValue());
        bingRenXXDto.setChuShengDi(address.getText().getValue());
    }


}
